<?php

class Application_Form_BenutzerAnlegenForm extends Zend_Form
{
	
	protected $_status;
	protected $_passwort;
	protected $_name;
	protected $_vorname;
    
	public function __construct($options = null, $passwort = '', $name= '' , $vorname= '', $status = '')
    {
		$this->_passwort= $passwort;
    	$this->_name = $name;
        $this->_vorname= $vorname;
    	$this->_status = $status;
        parent::__construct($options);
    }
    

    public function init()
    {
    	$decorator = new My_Decorator_SimpleInput();
    	$decorator2 = new My_Decorator_SimpleInput2();
    	$decorator3 = new My_Decorator_SimpleSelect();
    	
    	$this->setName('Benutzer erstellen');
        // Set the method for the display form to POST
        $this->setMethod('post');

        // Passwort
        $passwort  = new Zend_Form_Element('passwort', array(
        'label'      => 'Passwort',
        'value'      => $this->_passwort,
        'validators' => array(
                array('validator' => 'StringLength', 'options' => array(1, 10))
                )
    	));
    	
    	$passwort
    	->setRequired()
    	->addErrorMessage('Fehler bei der Passworteingabe');
    	$passwort
    	->clearDecorators()
    	->addDecorator($decorator2)
    	->addDecorator('Errors');
       
        // Name
        $name  = new Zend_Form_Element('name', array(
        'label'      => 'Name',
        'value'      => $this->_name,
        'filters'    => array('StringTrim'),
        'validators' => array(
                array('validator' => 'StringLength', 'options' => array(1, 50))
                )
    	));
    	
    	$name
    	->setRequired()
    	->addErrorMessage('Fehler bei der Namenseingabe');
    	$name
    	->clearDecorators()
    	->addDecorator($decorator)
    	->addDecorator('Errors');
    
        // Vorname
        $vorname  = new Zend_Form_Element('vorname', array(
        'label'      => 'Vorname',
        'value'      => $this->_vorname,
        'filters'    => array('StringTrim'),
        'validators' => array(
                array('validator' => 'StringLength', 'options' => array(1, 50))
            )
    	));
    	
    	$vorname
    	->setRequired()
    	->addErrorMessage('Fehler bei der Vornameneingabe');
    	$vorname
    	->clearDecorators()
    	->addDecorator($decorator)
    	->addDecorator('Errors');
    	
		//Auswahlliste
		$status  = new Zend_Form_Element_Select('status', array(
        'label'      => 'Status',
        'value'      => $this->_status,
        'MultiOptions' => array(
       				'Admin' => 'Administrator',
       				'Stud' => 'Student',
       				'Extern' => 'Extern'),
       			));
    	
    
    	$status
    	->setRequired()
    	->addErrorMessage('Fehler bei der Statuseingabe');
    	//wieder aktivieren falls Decorator vorhanden
    	$status
    	->clearDecorators()
    	->addDecorator($decorator3)
    	->addDecorator('Errors');
    	      
        // Submit button
        $submit = new Zend_Form_Element_Submit('Speichern',array('class' => 'button'));
		$submit->setLabel('Speichern');
		
		$form = new Zend_Form();

		$this->addElements(array($passwort,$name,$vorname,$status,$submit));
    }
}

	class My_Decorator_SimpleInput extends Zend_Form_Decorator_Abstract
    {
        protected $_format = '<p><label for="%s">%s</label><input id="%s" name="%s" type="text" value="%s"/></p>';
     
        public function render($content)
        {
            $element = $this->getElement();
            $name    = htmlentities($element->getFullyQualifiedName());
            $label   = htmlentities($element->getLabel());
            $id      = htmlentities($element->getId());
            $value   = htmlentities($element->getValue());
     
            $markup  = sprintf($this->_format, $name, $label, $id, $name, $value);
            return $markup;
        }
    }
	//Decorator für Passwordfeld 
	class My_Decorator_SimpleInput2 extends Zend_Form_Decorator_Abstract
    {
        protected $_format = '<p><label for="%s">%s</label><input id="%s" name="%s" type="password" value="%s"/></p>';
     
        public function render($content)
        {
            $element = $this->getElement();
            $name    = htmlentities($element->getFullyQualifiedName());
            $label   = htmlentities($element->getLabel());
            $id      = htmlentities($element->getId());
            $value   = htmlentities($element->getValue());
     
            $markup  = sprintf($this->_format, $name, $label, $id, $name, $value);
            return $markup;
        }
    }
    //Decorator für Selectfeld
    class My_Decorator_SimpleSelect extends Zend_Form_Decorator_Abstract
    {
        protected $_format = '<label for="%s">%s</label>';
     
        public function render($content)
        {
            $element = $this->getElement();
            $name    = htmlentities($element->getFullyQualifiedName());
            $label   = htmlentities($element->getLabel());
            $id      = htmlentities($element->getId());
            $value   = htmlentities($element->getValue());
            $options = $element->getMultiOptions();
     
            $markup  = sprintf($this->_format, $name, $label);
            $markup  = $markup . '<select name="' . $name . '" id="' . $id .'">';
        	foreach ($options as $intern => $extern) {
	     		$markup .= '<option value="'. $intern . '" label="' .$extern .'">' . $extern . '</option>';
	     		
	     	}
            $markup .= '</select>';
            return $markup;
        }
    }
 
    
    
    
    
    